package com.jy.appmarket.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.TextUtils;

public class AppPackageInfo {
	
	public static final String TAG = "AppPackageInfo";

	public static String getUninstallAPKInfo(Context ctx,String archiveFilePath) {
		//archiveFilePath=Environment.getExternalStorageDirectory()+"/"+"TestB.apk"
		if(TextUtils.isEmpty(archiveFilePath)) {
			return "";
		}
		String versionName = "";
		PackageManager pm=ctx.getPackageManager();
		PackageInfo pakinfo=pm.getPackageArchiveInfo(archiveFilePath,PackageManager.GET_ACTIVITIES);
		if (pakinfo!=null) {
			versionName=pakinfo.versionName;
		}
		return versionName;
	}
	
	public static String getInstallAPKInfo(Context ctx) {
		//pak=com.nmbb.b
		String versionName = null;
		PackageManager pm=ctx.getPackageManager();
		PackageInfo pakinfo;
		try {
			pakinfo = pm.getPackageInfo(ctx.getPackageName(),PackageManager.GET_ACTIVITIES);
			if (pakinfo!=null) {
				versionName=pakinfo.versionName;
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return versionName;
	}
	
	public static ApplicationInfo getApplicationInfo(Context context, String pkgName){
		ApplicationInfo ai = null;
		try {
			ai = context.getPackageManager().getApplicationInfo(pkgName, 0);
		} catch (NameNotFoundException e) {
			LogUtil.e(TAG, pkgName + ":ApplicationInfo NameNotFoundException!");
			e.printStackTrace();
		}
		return ai;
	}
}
